import { check } from 'meteor/check';
import { Messages } from '/imports/models/Messages';

Meteor.methods({
  sendMessage(options) {
    check(options.receiver, String);
    check(options.details, Object);

    Messages.insert({
      user_id: Meteor.userId(),
      friend_id: options.receiver,
      sender: Meteor.userId(),
      receiver: options.receiver,
      details: options.details,
      status: 'unread'
    });
    
    Messages.insert({
      user_id: options.receiver,
      friend_id: Meteor.userId(),
      sender: Meteor.userId(),
      receiver: options.receiver,
      details: options.details,
      status: 'unread'
    });

    return true;
  },

  deleteMessage(options) {
    check(options.id, String);

    Messages.remove({
      _id: options.id
    });

    return true;
  },

  deleteMessages(options) {
    check(options.userId, String);
    check(options.friendId, String);

    Messages.remove({
      user_id: options.userId,
      friend_id: options.friendId
    });

    return true;
  },

  changeStatus(options) {
    check(options._id, String);

    Messages.update({
      _id: options._id
    },{
      $set: { status: 'read' },
    });
    
    return true;
  }
});
